package org.flower.web.servlet;

import org.flower.bean.Order;
import org.flower.entity.User;
import org.flower.service.OrderService;
import org.flower.service.UserService;
import org.flower.service.impl.OrderServiceImpl;
import org.flower.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value = "/orderDeleteServlet.shtml")
public class orderDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        User user = (User)request.getSession().getAttribute("user");
        OrderService orderService = new OrderServiceImpl();
        List<Order> list =orderService.getAllById(user.getId());
        if (list!=null&&list.size()>0){
            for (Order o: list) {
                if(o.getId().equals(id)){
                    orderService.DeleteDing(id);
                    request.getRequestDispatcher("order.jsp").forward(request,response);
                    return;
                }
            }
        }
        request.getRequestDispatcher("order.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doGet(request,response);
    }
}
